package com.xinbochuang.machine.admin.controller.system;


import cn.hutool.core.date.DateUtil;
import com.xinbochuang.machine.admin.domain.system.SysDictData;
import com.xinbochuang.machine.admin.service.system.ISysDictDataService;
import com.xinbochuang.machine.admin.service.system.ISysDictTypeService;
import com.xinbochuang.machine.common.controller.BaseController;
import com.xinbochuang.machine.common.domain.TableDataInfo;
import com.xinbochuang.machine.common.utils.SecurityUtils;
import com.xinbochuang.machine.framework.web.domain.AjaxResult;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 数据字典信息
 *
 * @author 黄晓鹏
 * @date 2020-09-18
 */
@RestController
@RequestMapping("/system/dict/data")
public class SysDictDataController extends BaseController {

    @Resource
    private ISysDictDataService dictDataService;

    @Resource
    private ISysDictTypeService dictTypeService;

    /**
     * 字典列表
     *
     * @param dictData 字典
     * @return 结果
     */
    @PreAuthorize("@ss.hasPermi('system:dict:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysDictData dictData) {
        return getDataTable(dictDataService.selectDictDataList(dictData));
    }

    /**
     * 查询字典数据详细
     */
    @PreAuthorize("@ss.hasPermi('system:dict:query')")
    @GetMapping(value = "/{dictCode}")
    public AjaxResult getInfo(@PathVariable Integer dictCode) {
        return AjaxResult.data(dictDataService.getById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType) {
        return AjaxResult.data(dictTypeService.selectDictDataByType(dictType));
    }

    /**
     * 新增字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:add')")
    @PostMapping
    public AjaxResult add(@RequestBody SysDictData dict) {
        dict.setCreateBy(SecurityUtils.getUsername());
        dict.setCreateTime(DateUtil.now());
        return toAjax(dictDataService.insertDictData(dict));
    }

    /**
     * 修改保存字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:edit')")
    @PutMapping
    public AjaxResult edit(@RequestBody SysDictData dict) {
        dict.setUpdateBy(SecurityUtils.getUsername());
        dict.setUpdateTime(DateUtil.now());
        return toAjax(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典类型
     */
    @PreAuthorize("@ss.hasPermi('system:dict:remove')")
    @DeleteMapping("/{dictCodes}")
    public AjaxResult remove(@PathVariable Integer[] dictCodes) {
        return toAjax(dictDataService.deleteDictDataByIds(dictCodes));
    }


//    /**
//     * 根据字典类型查询字典数据信息
//     */
//    @GetMapping(value = "/dictTypeByApp")
//    public AjaxResult dictTypeByApp(@RequestParam("dictType")  String dictType) {
//        return AjaxResult.data(dictTypeService.selectDictDataByType(dictType));
//    }
}

